package com.egao.common.manage.student;

import com.egao.common.component.minio.config.MinioBucket;
import com.egao.common.component.minio.service.MinioService;
import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.module.world.entity.WorldGameStudent;
import com.egao.common.module.world.service.WorldGameStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lym
 * @date 2023/4/18 13:38
 */
@Api(tags = "学生济世杯管理")
@RestController
@RequestMapping("${api}")
public class StudentWorldGameController extends BaseController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private WorldGameStudentService worldGameStudentService;
    @Autowired
    private MinioService minioService;

    @PreAuthorize("hasAuthority('student')")
    @ApiOperation(value = "我报名的济世杯")
    @ApiPageParam
    @GetMapping("/student/world-game/page")
    public PageResult<WorldGameStudent> page(HttpServletRequest request) {
        Student student = studentService.getByUserId(getLoginUserId());
        if (student == null) {
            return new PageResult<>();
        }

        PageParam<WorldGameStudent> pageParam = new PageParam<>(request);
        pageParam.put("studentId", student.getId());
        List<WorldGameStudent> list = worldGameStudentService.listPage(pageParam);

        for (WorldGameStudent item : list) {
            String fileUrl = minioService.getPreSignedObjectUrl(MinioBucket.MINIO_WORLD, item.getFileUrl());
            item.setFileUrl(fileUrl);
        }

        return new PageResult<>(list);
    }

}
